package com.whfc.ms.api.fuum.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.fuum.entity.AppContract;
import com.whfc.fuum.service.AppContractService;
import com.whfc.ms.api.fuum.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合同管理服务实现类
 *
 * @author dzg
 * @since 2025-09-23
 */
@Slf4j
@Service
public class ContractServiceImpl implements ContractService {

    @DubboReference(interfaceClass = AppContractService.class, version = "1.0.0")
    private AppContractService appContractService;

    @Override
    public AppContract getById(Integer id) {
        AppContract contract = appContractService.getById(id);
        if (contract == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "合同不存在");
        }
        return contract;
    }

    @Override
    public List<AppContract> list(Integer deptId, String contractName) {
        return appContractService.list(deptId, contractName);
    }

    @Override
    public boolean save(AppContract contract) {
        boolean result = appContractService.save(contract);
        if (!result) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "新增失败");
        }
        return result;
    }

    @Override
    public boolean update(AppContract contract) {
        AppContract existingContract = appContractService.getById(contract.getId());
        if (existingContract == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "合同不存在");
        }
        boolean result = appContractService.update(contract);
        if (!result) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "更新失败");
        }
        return result;
    }

    @Override
    public boolean delete(Integer id) {
        boolean result = appContractService.delete(id);
        if (!result) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "删除失败");
        }
        return result;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = appContractService.deleteBatch(ids);
        if (!result) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "批量删除失败");
        }
        return result;
    }
}